<?php
namespace app\admin\controller;

use app\common\component\user\UserModel;
use app\common\model\UserPlatoon as UserPlatoonModel;
use app\common\model\UserPlatoonLog as UserPlatoonLogModel;

use app\common\component\user\UserService;
use app\common\component\user_platoon\UserPlatoonService;
use app\common\component\user_platoon_log\UserPlatoonLogService;

class UserPlatoon extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();

		$this -> checkAuthorization();
	}

	public function check() {
		$this -> view -> assign('userPlatoonLogs', UserPlatoonLogModel::order('is_state ASC, user_platoon_log_id DESC') -> paginate(10));

		return $this -> view -> fetch();
	}

	public function handleCheck() {
		$getUserPlatoonLog = UserPlatoonLogModel::get(['user_platoon_log_id' => $this -> request -> get('user_platoon_log_id', 0, 'int')]);

		$findUser = UserModel::where('username', $getUserPlatoonLog['to_user_name']) -> find();
		if(!$findUser) {
			throw new \Exception('用户不存在');
		}

		$resultGetUserPlatoon = (new UserPlatoonService) -> getUserPlatoonId($findUser['user_id']);

		$getUserPlatoonLog -> to_user_name_new_price = $resultGetUserPlatoon['data']['user_platoon'] -> price + $getUserPlatoonLog['price'];
		$getUserPlatoonLog -> to_user_name_old_price = $resultGetUserPlatoon['data']['user_platoon'] -> price;
		$getUserPlatoonLog -> is_state = 1;

		\think\Db::startTrans();
		try {
			if($getUserPlatoonLog -> save()) {
				if(!UserPlatoonModel::where('user_id', $findUser['user_id']) -> setInc('price', $getUserPlatoonLog['price'])) {
					throw new \Exception('审核失败');
				}
			} else {
				throw new \Exception('审核失败');
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();
			echo '<meta charset="utf-8"><script>alert("' . $e -> getMessage() . '");window.location.href="/shuashuale/user/platoon/check";</script>';exit;
		}
		echo '<meta charset="utf-8"><script>alert("审核成功");window.location.href="/shuashuale/user/platoon/check";</script>';exit;
	}

	public function topUp() {
		return $this -> view -> fetch();
	}

	public function handleTopUp() {
		$post = $this -> request -> post();

		$findUser = UserModel::where('username', $post['to_user_name']) -> find();
		if(!$findUser) {
			echo '<meta charset="utf-8"><script>alert("用户不存在");window.location.href="/shuashuale/user/platoon/topUp";</script>';exit;
		}
		
		\think\Db::startTrans();
		try {
			$resultUserPlatoonLogServiceLog = (new UserPlatoonLogService()) -> log(1, 1, $post['price'], $post['to_user_name'], $post['to_user_name'], '后台排单币充值', 1);
			if($resultUserPlatoonLogServiceLog['code'] != 0) {
				throw new \Exception($resultUserPlatoonLogServiceLog['message']);
			}

			if(!UserPlatoonModel::where('user_id', $findUser['user_id']) -> setInc('price', $post['price'])) {
				throw new \Exception('排单币充值失败');
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();
			echo '<meta charset="utf-8"><script>alert("' . $e -> getMessage() . '");window.location.href="/shuashuale/user/platoon/topUp";</script>';exit;
		}

		echo '<meta charset="utf-8"><script>alert("排单币充值成功");window.location.href="/shuashuale/user/platoon/topUp";</script>';exit;
	}
}